package com.zzg.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zzg.bean.Manage;
import com.zzg.service.ManageService;
import com.zzg.service.impl.ManageServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@WebServlet("/login.do")
public class ManageController extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		ManageService manageService =context.getBean(ManageService.class);
		String name =req.getParameter("name");
		String password =req.getParameter("password");
		String code = req.getParameter("code");
		
		String sCode = req.getSession().getAttribute("code").toString();
		//判断验证码是否正确
		if(code== null ||!sCode.equals(code.toUpperCase())){
			req.setAttribute("msg","验证码错误");
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			return;
		}
		Manage manage = manageService.login(name, password);
		//判断用户名密码是否一致
		if(manage != null){
			req.getSession().setAttribute("Manage", manage);
			req.getRequestDispatcher("/user.do?method=selectUserPage").forward(req, resp);
		}else {
			req.setAttribute("msg","用户名或密码错误");
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
		}
		

	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		doGet(req, resp);
	}
}
